package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.mybatisPlus.BaseServiceImpl;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.mapper.SysOperLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.mapper.SysPostMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.service.ISysPostService;

/**
 * 岗位信息 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysPostServiceImpl extends BaseServiceImpl<SysPostMapper, SysPost> implements ISysPostService
{
    @Autowired
    private SysPostMapper postMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    /**
     * 查询岗位信息集合
     * 
     * @param post 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public List<SysPost> selectPostList(SysPost post)
    {
//        return postMapper.selectPostList(post);
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<SysPost>();
        queryWrapper.like(ObjectUtil.isNotEmpty(post.getPostCode()),SysPost::getPostCode, post.getPostCode());
        queryWrapper.like(ObjectUtil.isNotEmpty(post.getPostName()),SysPost::getPostName, post.getPostName());
        queryWrapper.eq(ObjectUtil.isNotEmpty(post.getStatus()),SysPost::getStatus, post.getStatus());
        return postMapper.selectList(queryWrapper);
    }

    /**
     * 查询所有岗位
     * 
     * @return 岗位列表
     */
    @Override
    public List<SysPost> selectPostAll()
    {
//        return postMapper.selectPostAll();
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<SysPost>();
        return postMapper.selectList(queryWrapper);
    }

    /**
     * 通过岗位ID查询岗位信息
     * 
     * @param postId 岗位ID
     * @return 角色对象信息
     */
    @Override
    public SysPost selectPostById(Long postId)
    {
//        return postMapper.selectPostById(postId);
        return postMapper.selectById(postId);
    }

    /**
     * 根据用户ID获取岗位选择框列表
     * 
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Long> selectPostListByUserId(Long userId)
    {
//        return postMapper.selectPostListByUserId(userId);
        MPJLambdaWrapper<SysPost> wrapper = new MPJLambdaWrapper<SysPost>()
                .select(SysPost::getPostId)
                .distinct()
                .leftJoin(SysUserPost.class, SysUserPost::getPostId, SysPost::getPostId)
                .leftJoin(SysUser.class, SysUser::getUserId, SysUserPost::getUserId)
                .eq(SysUser::getUserId, userId);
        List<SysPost> sysPosts = postMapper.selectList(wrapper);
        return sysPosts.stream().map(SysPost::getPostId).collect(Collectors.toList());
    }

    /**
     * 校验岗位名称是否唯一
     * 
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostNameUnique(SysPost post)
    {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
//        SysPost info = postMapper.checkPostNameUnique(post.getPostName());
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<SysPost>();
        queryWrapper.eq(SysPost::getPostName, post.getPostName());
        SysPost info = postMapper.selectOne(queryWrapper);
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     * 
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostCodeUnique(SysPost post)
    {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
//        SysPost info = postMapper.checkPostCodeUnique(post.getPostCode());
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<SysPost>();
        queryWrapper.eq(SysPost::getPostCode, post.getPostCode());
        SysPost info = postMapper.selectOne(queryWrapper);
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 通过岗位ID查询岗位使用数量
     * 
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int countUserPostById(Long postId)
    {
//        return userPostMapper.countUserPostById(postId);
        LambdaQueryWrapper<SysUserPost> queryWrapper = new LambdaQueryWrapper<SysUserPost>();
        return userPostMapper.selectCount(queryWrapper.eq(SysUserPost::getPostId, postId));
    }

    /**
     * 删除岗位信息
     * 
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int deletePostById(Long postId)
    {
//        return postMapper.deletePostById(postId);
        return postMapper.deleteById(postId);
    }

    /**
     * 批量删除岗位信息
     * 
     * @param postIds 需要删除的岗位ID
     * @return 结果
     */
    @Override
    public int deletePostByIds(Long[] postIds)
    {
        for (Long postId : postIds)
        {
            SysPost post = selectPostById(postId);
            if (countUserPostById(postId) > 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
//        return postMapper.deletePostByIds(postIds);
        return postMapper.deleteBatchIds(Arrays.asList(postIds));
    }

    /**
     * 新增保存岗位信息
     * 
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int insertPost(SysPost post)
    {
//        return postMapper.insertPost(post);
        return postMapper.insert(post);
    }

    /**
     * 修改保存岗位信息
     * 
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int updatePost(SysPost post)
    {
//        return postMapper.updatePost(post);
        return postMapper.updateById(post);
    }
}
